/*
* TTrackControllerPane.java
* Display pane for the Track Controller Module
* Author: Nikolas Parshook
* Date Created: 2012-12-13
* Date Last Updated: 2012-12-13
*/

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

/**
* Display pane for the Track Controller Module
* @author Nikolas Parshook
*/
public class TrackControllerPanel{
	public static String status = "Bundle:";
	public static JLabel lblBundle; 
	public TrackControllerModule tcm;
	JFrame frame;
	JLabel pan;
	JButton next;
	JButton previous;
	GridBagLayout gl = new GridBagLayout();
	public TrackControllerPanel(TrackControllerModule tcm) {
		this.tcm = tcm;
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	public void initialize() {
		frame = new JFrame("Track Controller Module");
		frame.setBounds(700, 0, 435, 160);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(gl);
		lblBundle = new JLabel(status);
		pan = new JLabel();
		GridBagConstraints c = new GridBagConstraints();
		c.weightx = 0.0;
		c.weighty = 0.1;
		c.gridwidth = GridBagConstraints.REMAINDER;
		gl.setConstraints(lblBundle, c);
		frame.getContentPane().add(lblBundle);
		setLabel();
		gl.setConstraints(pan, c);
		frame.getContentPane().add(pan);
		next = new JButton("Next TC");
		next.addActionListener(new ActionListener () {
			public void actionPerformed(ActionEvent e) {
				new Thread()
			     {
			        public void run()
			        {
			        	tcm.currentController++;
						if(tcm.currentController == tcm.tcs.length)
							tcm.currentController = 0;
						tcm.displayDate = new Date();
						//tcm.ctc.appending = tcm.ctc.appending + "\n" + tcm.format.format(tcm.displayDate) + ":  TRC ID: "+ new Integer(tcm.tcs[tcm.currentController].id).toString() ;
						//tcm.ctc.textAreaDisplay.setText(tcm.ctc.appending);
						//tcm.ctc.textAreaDisplay.setCaretPosition(tcm.ctc.textAreaDisplay.getDocument().getLength());
						setLabel();
			        }
			     }.start();
				
			}
		});
		previous = new JButton("Previous TC");
		previous.addActionListener(new ActionListener () {
			public void actionPerformed(ActionEvent e) {
				new Thread()
			     {
			        public void run()
			        {
			        	tcm.currentController--;
						if(tcm.currentController < 0)
							tcm.currentController = tcm.tcs.length - 1;
						tcm.displayDate = new Date();
						//tcm.ctc.appending = tcm.ctc.appending + "\n" + tcm.format.format(tcm.displayDate) + ":  TRC ID: "+ new Integer(tcm.tcs[tcm.currentController].id).toString() ;
						//tcm.ctc.textAreaDisplay.setText(tcm.ctc.appending);
						//tcm.ctc.textAreaDisplay.setCaretPosition(tcm.ctc.textAreaDisplay.getDocument().getLength());
						setLabel();
			        }
			     }.start();
				
			}
		});
		
		frame.getContentPane().add(previous);
		frame.getContentPane().add(next);
		frame.setVisible(true);
	}
	
	private void setLabel()
	{
		if(tcm.tcs[tcm.currentController].section.type == 2)
			pan.setText("Track Controller id: " + new Integer(tcm.tcs[tcm.currentController].id).toString() +"     Type: Switch, Block " + tcm.tcs[tcm.currentController].section.number + " switch to " + tcm.tcs[tcm.currentController].section.switchToNumber);
		else
		{
			if(tcm.tcs[tcm.currentController].section.crossingOn)
				pan.setText("Track Controller id: " + new Integer(tcm.tcs[tcm.currentController].id).toString() +"     Type: Crossing,  On");
			else
				pan.setText("Track Controller id: " + new Integer(tcm.tcs[tcm.currentController].id).toString() +"     Type: Crossing,  Off");
		}
	}
}
